#!/usr/bin/perl

use strict;
use Getopt::Long;
use Contents::Main::Transcode;
use Contents::Main::GetSubtitles;
use Contents::Main::TVs2iT;
use Contents::Main::help qw(help help_lang);

my $same_specie;
my $help_lang;
my $help;
my $input;
my $TVShow2iTunes;
my $Subtitles;
my @lang;

my $transcode;
GetOptions(
	"i=s" => \$input,
	"input=s" => \$input,
	"help" => \$help,
	"l=s" => \@lang,
	"lang=s" => \@lang,
	"help-lang" => \$help_lang,
	"t=s" => \$transcode,
	"transcode=s" => \$transcode
);

if($input){
	#input is a file
	if (-f $input) {
		$TVShow2iTunes=Contents::Main::TVs2iT->new(
			input=> $input,
			lang=>@lang,
			transcode=>$transcode
		);
		
		$Subtitles=Contents::Main::GetSubtitles->new($TVShow2iTunes);
		$Subtitles->login($TVShow2iTunes);		
		$TVShow2iTunes->main();
		$Subtitles->logout($TVShow2iTunes);
		
	}
	# #input is a folder
	elsif(-d $input){
		opendir ( DIR, $input ) || die "Error in opening dir $input\n";
		my @dir=readdir(DIR);
		print "LOG :: Reading the folder $input\n";
		my $loginc_cont=0;
		foreach my $filename (@dir){
			if($filename !~ /^\./){
				$loginc_cont++;
				$TVShow2iTunes=Contents::Main::TVs2iT->new(
					input=> $input."/". $filename,
					lang=>@lang,
					transcode=>$transcode
				);
				$Subtitles=Contents::Main::GetSubtitles->new($TVShow2iTunes);
				$Subtitles->login($TVShow2iTunes);
				$TVShow2iTunes->main();
				$Subtitles->logout($TVShow2iTunes);
			}
		}
		close DIR;
	}
	else{
		Contents::Main::Help->help();
	}
	
}elsif($help){
	Contents::Main::Help->help();
}elsif($help_lang){
	Contents::Main::Help->help_lang();
}
else{
	Contents::Main::Help->help();
}



